#include <stdio.h>
#include <string.h>
int judge(char c[])
{
    int x;
    if(strcmp(c,"Mon")==0) x = 1;
    else if(strcmp(c,"Tue")==0) x = 2;
    else if(strcmp(c,"Wen")==0) x = 3;
    else if(strcmp(c,"Thur")==0) x = 4;
    else if(strcmp(c,"Fri")==0) x = 5;
    else if(strcmp(c,"Sat")==0) x = 6;
    else if(strcmp(c,"Sun")==0) x = 7;
    return x;
}
int main()
{
    int k, n, t, i, cnt, flag;
    int s,e;
    int j,f;
    char c[6];
    char chi[7][8]={"0","Mon","Tue","Wen","Thur","Fri"};
    scanf("%d",&k);
    while(k--)
    {
        int time[12][8]={0};
        char ch[56][8]={0};
        int x[56]={0};
        int m;
        cnt = 0;
        scanf("%d %d",&n,&t);
        while(n--)
        {
            scanf(" %s %d %d",c,&s,&e);
            m = judge(c);
            for(i = s;i <= e;i++)
            {
                time[i][m] = 1;
            }
        }
        
        for(i = 1;i <= 5;i++)
        {
            for(j = 1;j+t-1<=4;j++)
            {    
                flag = 1;
                for(f = j;f-j<t;f++)
                {
                    if(time[f][i]==1)
                    {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                {
                    cnt++;
                    strcpy(ch[cnt],chi[i]);
                    x[cnt] = j;
                }
            }
            for(j = 5;j+t-1<=8;j++)
            {
                flag = 1;
                for(f = j;f-j<t;f++)
                {    
                    if(time[f][i]==1)
                    {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                {
                    cnt++;
                    strcpy(ch[cnt],chi[i]);
                    x[cnt] = j;
                }
            }
            for(j = 9;j+t-1<=11;j++)
            {
                flag = 1;
                for(f = j;f-j<t;f++)
                {
                    if(time[f][i]==1)
                    {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                {
                    cnt++;
                    strcpy(ch[cnt],chi[i]);
                    x[cnt] = j;
                }
            }
        }
        printf("%d\n",cnt);
        for(i = 1;i <= cnt;i++)
        {
            printf("%s %d %d\n",ch[i],x[i],x[i]+t-1);
        }
    }
}
